package de.hfu.model;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import de.hfu.helper.Helper;

public class DayPlan implements Serializable, Comparable<DayPlan> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 94358051318243907L;
	private Date date;
	private ArrayList<Dish> dishes;
	private String description;
	
	private boolean translated;

	public DayPlan() {
		date = new Date();
		dishes = new ArrayList<Dish>();
		description = "";
	}

	public DayPlan(Date date) {
		this.date = date;
		dishes = new ArrayList<Dish>();
	}

	public void setDate(String date) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("E, dd.MM.yyyy",
				Locale.GERMANY);
		this.date = format.parse(date);
	}

	public void setDate(Date date) {
		this.date = date;
		for (Dish x : dishes) {
			x.setDate(date);
		}
	}

	public Date getDate() {
		return this.date;
	}

	public ArrayList<Dish> getDishes() {
		return this.dishes;
	}

	public void setDishes(ArrayList<Dish> dishes) {
		this.dishes = dishes;
	}

	public void addDish(Dish dish) {
		this.dishes.add(dish);
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	@Override
	public String toString() {
		String result = date.toString() + "\n";
		for (Dish x : this.dishes)
			result += x + "\n\n";

		return result;
	}

	@Override
	public int compareTo(DayPlan another) {
		int tmp = this.date.compareTo(another.date);
		if (tmp != 0)
			return tmp;

		tmp = this.dishes.size() - another.dishes.size();
		if (tmp != 0)
			return tmp;
		else
			for (int i = 0; i < this.dishes.size(); i++) {
				tmp = this.dishes.get(i).compareTo(another.dishes.get(i));
				if (tmp != 0)
					return tmp;
			}
		return this.description.compareTo(another.description);
	}

	@Override
	public boolean equals(Object o) {
		DayPlan tmp = (DayPlan) o;
		boolean ck = Helper.isEqual(this.date, tmp.date);
		if (!ck)
			return false;

		ck = this.dishes.size() == tmp.dishes.size();
		if (!ck)
			return false;

		for (int i = 0; i < this.dishes.size(); i++)
			if (!this.dishes.get(i).equals(tmp.dishes.get(i))) {
				return false;
			}
		return true;
	}

	public boolean isTranslated() {
		return translated;
	}

	public void setTranslated(boolean translated) {
		this.translated = translated;
	}

}
